package cc.ding.dao.Impl;

import cc.ding.dao.BaseDao;
import cc.ding.dao.EnrollDao;
import cc.ding.entity.Department;
import cc.ding.entity.Employee;
import cc.ding.entity.Position;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EnrollDaoImpl extends BaseDao implements EnrollDao {
    @Override
    public List<Department> selectDepart() {
        String sql = "SELECT * FROM department where status = 1";
        ResultSet resultSet = executeQuery(sql);
        List<Department> departmentList = new ArrayList<>();
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    Department department = new Department();
                    department.setId(resultSet.getInt("id"));
                    department.setDeptName(resultSet.getString("deptName"));
                    departmentList.add(department);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closedAll(resultSet);
            }
        }
        return departmentList;
    }

    @Override
    public List<Position> selectPosition() {
        String sql = "SELECT * FROM position";
        ResultSet resultSet = executeQuery(sql);
        List<Position> positionList = new ArrayList<>();
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    Position position = new Position();
                    position.setId(resultSet.getInt("id"));
                    position.setPositionName(resultSet.getString("positionName"));
                    positionList.add(position);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closedAll(resultSet);
            }
        }
        return positionList;
    }

    @Override
    public int add(Employee employee) {
        StringBuilder sb = new StringBuilder();
        sb.append(" INSERT INTO");
        sb.append(" employee(employeeNo,name,imgPath,sex,");
        sb.append(" birthday,depId,positionId,phone,entryDate,email)");
        sb.append(" VALUES (?,?,?,?,?,?,?,?,NOW(),?)");
        return executeUpdate(sb.toString(), employee.getEmployeeNo(), employee.getName(),
                employee.getImgPath(), employee.getSex(), employee.getBirthday(),
                employee.getDepId(), employee.getPositionId(), employee.getPhone(), employee.getEmail());
    }
}
